草庐IT

c++ - 实现安全的 nullptr

全部标签

go - 简单的 Web 服务器实现 : GoLang

我正在尝试使用Go实现一个简单的Web服务器。我希望“HelloWorld”显示在客户端浏览器的URL“http://127.0.0.1.12000/”处。我尝试了以下代码,但以错误告终。packagemainimport"net"import"fmt"import"bufio"//import"strings"//onlyneededbelowforsampleprocessingfuncmain(){fmt.Println("Launchingserver...")//listenonallinterfacesln,err:=net.Listen("tcp",":12000")if

go - 如何知道结构或结构指针是否实现接口(interface)

我需要知道结构或指向该结构的指针是否实现了给定的接口(interface)。//Youcaneditthiscode!//Clickhereandstarttyping.packagemainimport"fmt"funcmain(){varaA=A{i:5,}Serialize(a)Serialize(&a)}typeSerializableinterface{//Serialize()string//Deserialize(string)Serializebyte()[]byteDeserializebyte(b[]byte)(bytesReadint)}typeAstruct{i

go - 我如何使用接口(interface)定义安全划分并反射(reflect)?

我想定义一个安全分区,例如:funcsafeDivide(a,binterface{})interface{}{ifb==0{return0}returna/b}很明显,这个功能是行不通的。我们不能划分界面。一种解决方案是判断输入的类型并进行除法。switchreflect.ValueOf(x).Kind(){casereflect.Int://balabala...虽然看起来很多余,但我必须处理每一个案子。那么我是否可以使用反射来保证输入的类型?我试过reflect.TypeOf()但失败了。顺便说一句,我注意到了这一点:a:=uint32(0)ifZero(a)//outputs"

go - 如果接口(interface)类型的变量是实现它的某种结构类型,如何检查 Go?

假设我有以下内容:typeAInterface{....}//ImplementsAtypeBstruct{....}//ImlementsAtypeCstruct{....}现在我有一个函数,它接受A类型的变量作为参数:funcFoo(objA){ifAisB{....}elseifAisC{....}}还有一个main函数:funcmain(){b:=B{}Foo(b)}如何检查传递给函数的参数是否实际上是B类型? 最佳答案 使用typeswitch如前所述,@CeriseLimón链接的旅游页面。funcFoo(vA){swi

go - 在 Go 中安全地获取 slice 的一部分

我知道您可以通过以下方式在Go中获取slice的一部分test:=[]int{0,1,2,3,4}subsection:=test[:2]但是如果你尝试类似的东西subsectionError:=test[:10]你会得到一个越界错误。目前我正在做类似的事情length:=10iflen(test)为了避免这个错误,但是有更好的方法吗?这对我来说感觉很老套,但话又说回来,Go通常似乎是一种非常明确的语言。 最佳答案 在Go中,答案通常是在函数或方法中隐藏丑陋之处。例如,一个min函数,packagemainimport"fmt"fu

在没有 switch 语句的情况下在运行时选择实现

我想使用提供的字符串在运行时选择接口(interface)的实现。我不想使用switch语句-代码应该是通用的,并且可以与实现接口(interface)的任何新结构一起使用而无需修改(打开/关闭)。假设我有以下结构:typeFooerinterface{Foo()}typeAstruct{}func(_*A)Foo(){fmt.Println("CallingA")}typeBstruct{}func(_*B)Foo(){fmt.Println("CallingB")}typeCstruct{}func(_*C)Foo(){fmt.Println("CallingC")}然后,我想做类

go - 如何模拟接口(interface)实现

我的界面.gotypeMyInterfaceinterface{fun1()stringfun2()intfun3()bool}funcFoo(miMyInterface)string{returnmi.fun1()}我的接口(interface)测试.gotypeMyInterfaceImplementationstruct{}func(miMyInterfaceImplementation)fun1()string{return"foobar"}func(miMyInterfaceImplementation)fun2()int{returnint(100)}func(miMyIn

go - 如何使用构建器模式构造动态实现接口(interface)的结构

我正在尝试使用builderpatterns(从Java借来的)允许结构实现接口(interface)。例如,理想情况下我会喜欢这种代码模式:packagemainimport"fmt"typeOnerinterface{One()int}typeTwoerinterface{Two()int}funcmain(){s:=NewObject().WithOne(1).Build()_,ok:=s.(Oner)fmt.Println(ok)//Printstrue_,ok=s.(Twoer)fmt.Println(ok)//Printsfalset:=NewObject().WithOn

go - 如何优雅地使 slice 追加安全

slice:=[]int{10,20,30,40,50,60}newSlice:=slice[2:4:5]fmt.Printf("oldsliceis%d\n",slice)fmt.Printf("newsliceis%d\n",newSlice)newSlice=append(newSlice,70)fmt.Printf("oldsliceis%d\n",slice)fmt.Printf("newsliceis%d\n",newSlice)newSlice=append(newSlice,80)fmt.Printf("oldsliceis%d\n",slice)fmt.Printf(

go - 如何实现子路由

我想实现这样的路线用户/个人资料用户/购物车用户/产品目前,我正在做这件事r.HandleFunc("user/signup",signupHandler).Methods("POST")r.HandleFunc("user/signin",signinHandler).Methods("POST")r.HandleFunc("user/profile",profileHandler).Methods("GET")r.HandleFunc("user/cart",cartHandler).Methods("POST")r.HandleFunc("user/products",produ